package com.woniu40.petai_manager.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu40.petai_manager.entity.Goods;
import com.woniu40.petai_manager.mapper.GoodsMapper;
import com.woniu40.petai_manager.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    public PageInfo<GoodsVo> findGoodsByPage(Integer pageNum,Integer pageSize,String goodsName,Integer brand){
        PageHelper.startPage(pageNum,pageSize);
        List<GoodsVo> goodsVoList = goodsMapper.selectGoodsByTips(goodsName,brand);
        for (GoodsVo goodsVo : goodsVoList) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = simpleDateFormat.format(goodsVo.getCreateTime());
            goodsVo.setDateStr(dateStr);
        }
        return new PageInfo<>(goodsVoList);
    }

    public int addGoods(Goods goods){
        return goodsMapper.insert(goods);
    }

    public Integer findMidByCode(Integer userCode) {
        return goodsMapper.selectMidByCode(userCode);
    }

    public int modifyGoods(Goods goods) {
        return goodsMapper.updateByPrimaryKey(goods);
    }

    public int removeByIds(List<Integer> idList) {
        return goodsMapper.deleteByPrimaryKeys(idList);
    }

    public int removeById(Integer id) {
        return goodsMapper.deleteByPrimaryKey(id);
    }

    public Goods findGoodsById(Integer goodsId) {
        return goodsMapper.selectByPrimaryKey(goodsId);
    }
}
